package com.simplegui.examples.pong;

import static com.simplegui.examples.pong.PongConfigurations.FRAME_HEIGHT;

import java.awt.Color;
import java.awt.Point;

import com.simplegui.api.Canvas;
import com.simplegui.api.GameComponent;

public class Paddle implements GameComponent {
	
	public final int width;
	public final int height;
	public final int halfHeight;
	
	private Pong pong;
	private int position;
	private int velocity;
	private boolean automatic;
	private Color color;
	private int left;
	
	public Paddle(Pong pong, Color color, int left, boolean automatic) {
		width = 8;
		height = 80;
		halfHeight = height / 2;
		int initialPaddlePosition = FRAME_HEIGHT / 2 - halfHeight;
		position = initialPaddlePosition;
		
		velocity = 0;
		this.pong = pong;
		this.color = color;
		this.left = left;
		this.automatic = automatic;
	}

	public void decrementVelocity() {
		velocity += -1;
	}

	public void incrementVelocity() {
		velocity += 1;
	}

	@Override
	public void executeLogic() {
		// update paddle vertical position, keep paddle on the screen
		int newPosition = position + velocity * 5;
		if (automatic){
			newPosition = (int)pong.getBall().getBallPos()[1];
		}
		if (newPosition < 0){
			newPosition = 0;
		}else if(newPosition > FRAME_HEIGHT - height){
			newPosition = FRAME_HEIGHT - height;
		}
		position = newPosition;
	}
	
	public int getTopPosition() {
		return position;
	}
	
	public int getVelocity() {
		return velocity;
	}

	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}

	@Override
	public void draw(Canvas c) {
		c.drawLine(new Point(left, getTopPosition()), new Point(left, getTopPosition() + getHeight()), getWidth() * 2 + 1, color);
	}

	@Override
	public Point getPosition() {
		return new Point(left, getTopPosition());
	}

	@Override
	public double getRadius() {
		return 0;
	}
}
